import { store } from "@/stores";
import { defineStore } from "pinia";
import type { DrawerState } from "./helper";
import {
  defaultSetting,
  getLocalState,
  removeLocalState,
  setLocalState,
} from "./helper";

export const useDrawerStore = defineStore("drawer-storage", {
  state: (): DrawerState => getLocalState(),
  actions: {
    updateDrawer(drawer: Partial<DrawerState>) {
      this.$state = { ...this.$state, ...drawer };
      this.recordState();
    },

    resetDrawer() {
      this.$state = defaultSetting();
      removeLocalState();
    },

    recordState() {
      setLocalState(this.$state);
    },
  },
});

export function useDrawerStoreHook() {
  return useDrawerStore(store);
}
